package com.yuhs.zixing.study.multithread.mashibing.t001base.t004.method;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * Function: 线程方法 001 join
 * join 就是在自己当前线程加入你调用join的线程，本线程等待，等待调用的线程运行完毕，本线程才执行。
 * A、B两个线程， A在某个时间调用了B.join,A线程就会等待B线程执行完毕，A才会继续执行。
 *
 * @Author yuhongsheng
 * @Date: 2023-12-18 22:25:17
 */
@Slf4j
public class ThreadMethod003Join {


    public static void main(String[] args) {
        testJoin();
    }

    @SneakyThrows
    private static void testJoin() {
        Thread threadA = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                log.atInfo().log("测试join 线程A {}", i);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread threadB = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                log.atInfo().log("测试join 线程B {}", i);
                if (i % 5 != 0) {
                    try {
                        threadA.join();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });

        threadA.start();
        threadB.start();

    }


}
